package com.example.outofschool;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;
import cn.jiguang.analytics.android.api.LoginEvent;

public class MainFragment extends Fragment {
    private MyViewModel myViewModel;
    private String name, id, academy, location;
    Button buttonOut, buttonIn;
    EditText editTextName, editTextId, editTextAcademy;
    CheckBox checkBoxRemember;
    RadioButton radioButtonOld, radioButtonNew;
    RadioGroup radioGroup;
    private SharedPreferences.Editor editor;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment, container, false);
        buttonIn = view.findViewById(R.id.buttonIn);
        buttonOut = view.findViewById(R.id.buttonOut);
        editTextName = view.findViewById(R.id.editTextName);
        editTextId = view.findViewById(R.id.editTextId);
        editTextAcademy = view.findViewById(R.id.editTextAcademy);
        checkBoxRemember = view.findViewById(R.id.checkBoxRemember);
        radioButtonNew = view.findViewById(R.id.radioButtonNew);
        radioButtonOld = view.findViewById(R.id.radioButtonOld);
        radioGroup = view.findViewById(R.id.radioGroup);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);

        SharedPreferences shp = requireActivity().getSharedPreferences("My_Data", Context.MODE_PRIVATE);
        editor = shp.edit();

        editTextName.setText(shp.getString("userName", ""));
        editTextId.setText(shp.getString("userId", ""));
        editTextAcademy.setText(shp.getString("userAcademy", ""));
        checkBoxRemember.setChecked(shp.getBoolean("check", false));

        buttonIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                serOnClick(v,"入校");
            }
        });

        buttonOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               serOnClick(v,"出校");
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onStart() {
        super.onStart();
        /* 自适应status bar */
        Window window = requireActivity().getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.getDecorView().setSystemUiVisibility(ViewGroup.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(requireActivity(), this.getClass().getCanonicalName());
    }

    @Override
    public void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(requireActivity(), this.getClass().getCanonicalName());
    }

    private void serOnClick(View v,String type) {
        name = editTextName.getText().toString();
        id = editTextId.getText().toString();
        academy = editTextAcademy.getText().toString();
        location = radioGroup.getCheckedRadioButtonId() == R.id.radioButtonOld ? "崇文门" : "腾飞门";

        // 记住我
        if (checkBoxRemember.isChecked()) {
            editor.putBoolean("check", true);
            editor.putString("userName", name);
            editor.putString("userId", id);
            editor.putString("userAcademy", academy);
        } else {
            editor.clear();
        }
        editor.apply();

        /* 记录用户信息 */
        LoginEvent lEvent = new LoginEvent(type, true);
        lEvent.addKeyValue("姓名", name)
                .addKeyValue("学号", id)
                .addKeyValue("学院", academy)
                .addKeyValue("校门", location);
        JAnalyticsInterface.onEvent(requireActivity(), lEvent);

        myViewModel.setUserName(name);
        myViewModel.setUserId(id);
        myViewModel.setUserAcademy(academy);
        myViewModel.setLocation(location);
        myViewModel.setCrxType(type);
        NavController navController = Navigation.findNavController(v);
        navController.navigate(R.id.action_mainFragment_to_outFragment);
    }
}